<template>
    <div>
        <div>
            <el-card>
                <el-form class="searchForm" size="small" label-width="70px">
                    <el-form-item label="用户名称">
                        <el-select v-model="searchData.user" filterable placeholder="请选择">
                            <el-option
                                    v-for="item in userOptions"
                                    :key="item.id"
                                    :label="item.username"
                                    :value="item.username">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="登录时间">
                        <el-date-picker
                                v-model="times"
                                type="daterange"
                                range-separator="至"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期">
                        </el-date-picker>
                    </el-form-item>

                    <div class="searchBtn">
                        <el-button type="primary" size="small" @click="keywordSearch"><i class="el-icon-search"></i> 搜索</el-button>
                        <el-button type="info" size="small" @click="clean"><i class="el-icon-delete"></i> 清空</el-button>
                    </div>

                </el-form>
                <div>
                    <el-table
                            :data="tableData"
                            height="450"
                            border
                            style="width: 100%"
                            :cell-style="{'text-align' :'center'}"
                            :header-cell-style="{'text-align' :'center'}">
                        <el-table-column type="selection" width="55"></el-table-column>
                        <el-table-column
                                type="index"
                                label="序号"
                                width="50">
                        </el-table-column>
                        <el-table-column
                                prop="user"
                                label="用户名称"
                                width="180">
                        </el-table-column>
                        <el-table-column
                                label="登录状态">
                            <template slot-scope="scope">
                                {{scope.row.exception == null ?scope.row.user+'登录成功' : scope.row.exception}}
                            </template>
                        </el-table-column>
                        <el-table-column
                                prop="remoteAddr"
                                label="ip地址">
                        </el-table-column>
                        <el-table-column
                                prop="title"
                                label="操作信息">
                        </el-table-column>
                        <el-table-column
                                prop="createTime"
                                label="登录日期">
                        </el-table-column>
                        <el-table-column
                                label="操作">
                            <template slot-scope="scope">
                                <el-link type="primary" class="el-icon-view el-icon--right"  @click="see(scope.row)">查看
                                </el-link>
                                <el-link type="danger" class="btn" icon="el-icon-delete" @click="deleteLog(scope.row)">删除</el-link>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination
                            :total="pagingData.total"
                            background
                            layout="total,sizes, prev, pager, next"
                            :page-size="pagingData.size"
                            :page-sizes="[1, 5, 10, 20, 30, 50]"
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="pagingData.current"
                    ></el-pagination>
                    <el-dialog title="查看日志"  :visible.sync="seeDialogVisible" width="70%">
                        <el-form :model="logForm" label-position="left" label-width="100px" size="small">
                            <el-form-item label="用户名称">
                                {{logForm.user}}
                            </el-form-item>
                            <el-form-item label="登录状态">
                                {{logForm.exception == null ?logForm.user+'登录成功' : logForm.exception}}
                            </el-form-item>
                            <el-form-item label="操作信息">
                                {{logForm.title}}
                            </el-form-item>
                            <el-form-item label="请求接口路径">
                                {{logForm.requestUri}}
                            </el-form-item>
                            <el-form-item label="登录日期">
                                {{logForm.createTime}}
                            </el-form-item>
                        </el-form>
                        <span slot="footer" class="dialog-footer">
							<el-button size="mini" @click="seeDialogVisible = false">取 消</el-button>
							<el-button size="mini" type="primary" @click="seeDialogVisible=false">确 定</el-button>
						</span>
                    </el-dialog>

                </div>
            </el-card>
        </div>
    </div>
</template>

<script>

    import {deleteLog, findAllLogs} from "../../../../api/admin/system/log/operationLog";
    import moment from "moment";
    import {findUserPage} from "../../../../api/admin/permissions/user";

    export default {
        name: "loginLog",
        data() {
            return {
                searchData:{
                    name:'',
                    logType:'0',
                    startDate:'',
                    endDate:'',
                },
                times:'',
                tableData: [],
                // 分页数据
                pagingData: {
                    current: 1,
                    total: 0,
                    size: 20,
                },
                logForm:{},
                userOptions: [],
                seeDialogVisible:false

            }
        },
        created(){
            this.init();
            findUserPage({}).then(res=>{
                this.userOptions=res.data.data.records;
            })
        },
        methods:{
            init(){
                let search = {
                    ...this.searchData,
                    total: this.pagingData.total,
                    size: this.pagingData.size,
                    current: this.pagingData.current,
                }
                findAllLogs(search).then(res =>{
                    this.tableData=res.data.data.records;
                    this.pagingData.total = res.data.data.total
                    this.pagingData.size = res.data.data.size
                    this.pagingData.current = res.data.data.current
                })

            },
            keywordSearch(){
                if (this.times.length>0){
                    this.searchData.startDate=moment(this.times[0]).format('YYYY-MM-DD mm:ss');
                    this.searchData.endDate=moment(this.times[1]).format('YYYY-MM-DD mm:ss');
                }
                let search = {
                    ...this.searchData,
                    total: this.pagingData.total,
                    size: this.pagingData.size,
                    current: this.pagingData.current,
                }
                findAllLogs(search).then(res =>{
                    this.tableData=res.data.data.records;
                    this.pagingData.total = res.data.data.total
                    this.pagingData.size = res.data.data.size
                    this.pagingData.current = res.data.data.current
                })
            },
            see(row){
                this.logForm=row
                this.seeDialogVisible=true;
            },
            deleteLog(row){
                deleteLog(row.id).then(res=>{
                    if (res.data.code == 200) {
                        this.$message.success('删除日志成功')
                        this.init()
                    }else {
                        this.$message.error('删除日志失败')
                    }
                })
            },
            // 页码切换
            handleSizeChange(val) {
                this.pagingData.size = val
                this.keywordSearch()
            },
            // 当前页切换
            handleCurrentChange(val) {
                this.pagingData.current = val
                this.keywordSearch()
            },
            /**
             * 重置搜索框
             */
            clean() {
                this.searchData = {}
                this.times = []
            }
        }
    }
</script>

<style lang="scss" scoped>
    @import "~@/style/admin/system/log/loginLog.scss";
    @import "~@/style/publicPage.scss";

    a.btn {
        margin: 0 5px;
    }
</style>
